/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Dosen;
import java.beans.Statement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import rploo_ciayoo.kontrol.KoneksiDatabase;

public class DosenKontrol {
  private Connection conn;

    public DosenKontrol(Connection koneksi) {
        this.conn = koneksi;
    }

    public static DosenKontrol getKoneksi() throws SQLException {
        KoneksiDatabase konek = new KoneksiDatabase();
        DosenKontrol kon = new DosenKontrol(konek.getDBConnection());
        return kon;
    }

     public void tambahDosen(Dosen daftar) throws SQLException {
        PreparedStatement statement = null;
        try {
            conn.setAutoCommit(false);
            String sql = "insert into dosen "
                    + "(id_dosen,nama_dosen)"
                    + "values (?,?)";
            statement = conn.prepareStatement(sql);
            statement.setString(1, daftar.getId_dosen());
            statement.setString(2, daftar.getNama_dosen());
            statement.executeUpdate();
//             statement.executeQuery();
            conn.commit();
        } catch (SQLException exception) {
            conn.rollback();
            throw exception;
        } finally {
            try {
                conn.setAutoCommit(true);
                if (statement != null) {
                    statement.close();
                }
            } catch (SQLException exception) {
                throw exception;
            }
        }
    }

      public List<Dosen> lihatDataDosen() throws SQLException {
        PreparedStatement statement = null;
        ResultSet result = null;
        try {
            conn.setAutoCommit(false);

            statement = conn.prepareStatement("Select id_dosen, nama_dosen  "
                    + "from dosen "
                    + "order by id_dosen");

            result = statement.executeQuery();
            List<Dosen> data = new ArrayList<Dosen>();
            while (result.next()) {
                Dosen dsn = new Dosen();
                dsn.setId_dosen(result.getString(1));
                dsn.setNama_dosen(result.getString(2));

                data.add(dsn);
            }
            conn.commit();
            return data;
        } catch (SQLException exception) {
            conn.rollback();
            throw exception;
        } finally {
            try {
                conn.setAutoCommit(true);
                if (statement != null) {
                    statement.close();
                }
            } catch (SQLException exception) {
                throw exception;
            }
        }
    }
      public List<Dosen> getIdDosen() throws SQLException {
        PreparedStatement statement = null;
        ResultSet result = null;
        try {
            conn.setAutoCommit(false);

            statement = conn.prepareStatement("Select id_dosen from dosen order by id_dosen");

            result = statement.executeQuery();
            List<Dosen> data = new ArrayList<Dosen>();
            while (result.next()) {
                Dosen dsn = new Dosen();
                dsn.setId_dosen(result.getString(1));
//                dsn.setNama_dosen(result.getString(2));

                data.add(dsn);
            }
            conn.commit();
            return data;
        } catch (SQLException exception) {
            conn.rollback();
            throw exception;
        } finally {
            try {
                conn.setAutoCommit(true);
                if (statement != null) {
                    statement.close();
                }
            } catch (SQLException exception) {
                throw exception;
            }
        }
    }

     public void updateDosen(Dosen dsn) throws SQLException {
        PreparedStatement statement = null;
        conn.setAutoCommit(false);
//        Barang kodeBarang = stock.getKodebarang();
//        String kode = kodeBarang.getKodeBarang();
        String sql = "update dosen set nama_dosen = ? " 
                + "where id_dosen = ? ";
        statement = conn.prepareStatement(sql);
        statement.setString(1, dsn.getNama_dosen());
        statement.setString(2, dsn.getId_dosen());
    
        statement.executeUpdate();
        conn.commit();
    }



       public void hapusPrepared(Dosen dataDosen) throws SQLException {
        PreparedStatement statement = null;
        try {
            conn.setAutoCommit(false);
            String sql = "delete from dosen where id_dosen = ? and nama_dosen = ?";
            statement = conn.prepareStatement(sql);
            statement.setString(1, dataDosen.getId_dosen());
            statement.setString(2, dataDosen.getNama_dosen());
            statement.executeUpdate();
            conn.commit();
        } catch (SQLException ex) {
            conn.rollback();
            throw ex;
        } finally {
            try {
                conn.setAutoCommit(true);
                if (statement != null) {
                    statement.close();
                }
            } catch (SQLException ex) {
                throw ex;
            }
        }
    }

}
